<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\dynamic\model;

class DynamicFollow extends \app\BaseModel
{
    protected $name = "massage_dynamic_follow";
    protected $append = ["friend_time"];
    public function getFriendTimeAttr($value, $data)
    {
        if(isset($data["create_time"])) {
            return lbGetDates($data["create_time"]);
        }
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("status desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function getFollowList($dis, $page = 10)
    {
        $data = $this->alias("a")->join("massage_service_user_list b", "a.user_id = b.id", "left")->where($dis)->field("a.*,b.nickName,b.avatarUrl")->group("a.id")->order("a.create_time desc,a.id desc")->paginate($page)->toArray();
        return $data;
    }
    public function followCoachList($dis, $alh, $page = 10)
    {
        $data = $this->alias("a")->join("massage_service_coach_list b", "a.coach_id = b.id")->where($dis)->field(["a.*,b.coach_name,b.work_img,b.order_num", $alh])->group("a.coach_id")->order("distance,a.id desc")->paginate($page)->toArray();
        return $data;
    }
    public function followCoachNum($user_id)
    {
        $dis = ["a.user_id" => $user_id, "a.status" => 1, "b.status" => 2];
        $shield_model = new \app\massage\model\ShieldList();
        $coach_id = $shield_model->where(["user_id" => $user_id])->where("type", "in", [2, 3])->column("coach_id");
        $data = $this->alias("a")->join("massage_service_coach_list b", "a.coach_id = b.id")->where($dis)->where("a.coach_id", "not in", $coach_id)->group("a.coach_id")->count();
        return $data;
    }
    public function followUserNum($coach_id)
    {
        $dis = ["a.coach_id" => $coach_id, "a.status" => 1];
        $data = $this->alias("a")->join("massage_service_coach_list b", "a.coach_id = b.id")->where($dis)->group("a.id")->count();
        return $data;
    }
    public function getFollowDynamicNum($user_id, $where = [])
    {
        $shield_model = new \app\massage\model\ShieldList();
        $coach_id = $shield_model->where(["user_id" => $user_id])->column("coach_id");
        $dis = ["a.user_id" => $user_id, "b.status" => 2];
        $total_num = $this->alias("a")->join("massage_dynamic_list b", "a.coach_id = b.coach_id AND a.create_time < b.create_time")->join("massage_service_coach_list d", "a.coach_id = d.id")->where($dis)->where($where)->where("a.coach_id", "not in", $coach_id)->group("b.id")->count();
        $have_num = $this->alias("a")->join("massage_dynamic_list b", "a.coach_id = b.coach_id AND a.create_time < b.create_time")->join("massage_dynamic_watch_record c", "b.id = c.dynamic_id")->join("massage_service_coach_list d", "a.coach_id = d.id")->where($dis)->where($where)->where("a.coach_id", "not in", $coach_id)->group("b.id")->count();
        return 0 <= $total_num - $have_num ? $total_num - $have_num : 0;
    }
    public function getFollowDynamicData($user_id)
    {
        $dis = ["a.user_id" => $user_id, "b.status" => 2];
        $record_model = new DynamicWatchRecord();
        $id_arr = $record_model->where(["user_id" => $user_id])->column("dynamic_id");
        $data = $this->alias("a")->join("massage_dynamic_list b", "a.coach_id = b.coach_id AND a.create_time < b.create_time")->where($dis)->where("b.id", "not in", $id_arr)->field("b.id,a.uniacid")->group("b.id")->select()->toArray();
        return $data;
    }
}

?>